﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace EzDesk.Utility
{
    public static class EnumerableExt
    {
        public static IEnumerable<T> Foreach<T>(this IEnumerable<T> items, Action<T> method)
        {
            foreach (var item in items)
                method(item);
            return items;
        }

        public static IEnumerable<T> Foreach<T>(this IEnumerable<T> items, Action<T, int> method)
        {
            int i = 0;
            foreach (var item in items)
            {
                method(item, i);
                i++;
            }
            return items;
        }

        public static IEnumerable<TResult> Foreach<T, TResult>(this IEnumerable<T> items, Func<T, TResult> method)
        {
            List<TResult> results = new List<TResult>();
            foreach (var item in items)
               results.Add(method(item));
            return results;
        }

        public static IEnumerable<TResult> Foreach<T, TResult>(this IEnumerable<T> items, Func<T, int, TResult> method)
        {
            int i = 0;
            List<TResult> results = new List<TResult>();
            foreach (var item in items)
            {
                 results.Add(method(item, i));
                i++;
            }
            return results;
        }

        public static string[] ToStringArrary<T>(this IEnumerable<T> items)
        {
            return items.Select(i => i.ToString()).ToArray();
        }
        public static string Join<T>(this IEnumerable<T> items, string split)
        {
            return string.Join(split, items.ToStringArrary<T>());
        }
        public static IEnumerable<string> ToStringArrary<T>(this IEnumerable<T> items,Func<T,string> fun)
        {
            return items.Select(i => fun(i));
        }
        public static string Join<T>(this IEnumerable<T> items, string split, Func<T,string> fun)
        {
            return string.Join(split, items.ToStringArrary<T>(fun).ToArray());
        }
    }

    public static class DictionaryExt
    {
        public static Dictionary<TKey, TValue> AddRange<TKey, TValue>(this Dictionary<TKey, TValue> source, Dictionary<TKey, TValue> values)
        {
            foreach (var item in values)
            {
                source[item.Key] = item.Value;
            }
            return source;
        }

        public static Dictionary<TKey,TValue> AddRange<TKey,TValue>(this Dictionary<TKey,TValue> source, IEnumerable<KeyValuePair<TKey,TValue>> values)
        {
            foreach (var item in values)
            {
                source[item.Key] = item.Value;
            }
            return source;
        }
    }

    public static class EnumExt
    {
        
        public static string GetEnumName(this Enum enumType)
        {
            return Enum.GetName(enumType.GetType(), enumType);
        }
        public static IList<string> GetAllEnums(this Enum enumType)
        {
            return Enum.GetNames(enumType.GetType()).ToList();
        }

        public static T ParseEnum<T>(string enumString)
        {
            return (T)Enum.Parse(typeof(T), enumString);
        }
    }

    public static class XConvertExt
    {
        public static string ToXmlString(this object obj)
        {
            return obj.ToXmlString(false);
        }
        public static string ToXmlString(this object obj, bool omitXmlDeclaration)
        {
            XmlSerializer xmlS = new XmlSerializer(obj.GetType());
            StringBuilder xmlStr = new System.Text.StringBuilder();
            XmlWriterSettings setting = new XmlWriterSettings { OmitXmlDeclaration = omitXmlDeclaration };
            xmlS.Serialize(XmlWriter.Create(xmlStr, setting), obj);
            return xmlStr.ToString();
        }

        public static T GetFromXmlString<T>(this string xmlString)
        {
            XmlSerializer xmlS = new XmlSerializer(typeof(T));
            return (T)xmlS.Deserialize(XmlReader.Create((new StringReader(xmlString))));
        }
    }
   
}
